using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 模块
    /// </summary>
    public class ModuleControllerBase : ApiControllerBase
    {

        /// <summary>
        /// 模块接口
        /// </summary>
        protected readonly IModuleService _ModuleService;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public ModuleControllerBase(IModuleService moduleService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _ModuleService = moduleService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加模块
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "module:add")]
        [Log(Title = "模块", BusinessType = BusinessType.INSERT)]
        public virtual async Task AddModule([FromBody] ModuleFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<ModuleEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _ModuleService.AddModule(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改模块
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "module:edit")]
        [Log(Title = "模块", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ModifyModule([FromBody] ModuleFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<ModuleEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _ModuleService.ModifyModule(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除模块
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "module:delete")]
        [Log(Title = "模块", BusinessType = BusinessType.DELETE)]
        public virtual async Task DeleteModule([FromBody] DeleteModuleReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _ModuleService.DeleteModule(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }

        /// <summary>
        /// 变更模块状态
        /// </summary>
        /// <returns></returns>
        [HttpPost("changeState")]
        [ActionPermissionFilter(Permission = "module:changestate")]
        [Log(Title = "模块", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ChangeModuleState([FromBody] ChangeModuleStateReq req)
        {
            var isOk = await _ModuleService.ChangeModuleState(req);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }




        /// <summary>
        /// 查询模块树选择列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("treeSelectList")]
        [ActionPermissionFilter(Permission = "module:treeSelectList")]
        public virtual async Task<List<TreeNodeInfo>> GetModuleTreeSelectList([FromBody] GetModuleTreeSelectListReq req)
        {
            var result = await _ModuleService.GetModuleTreeSelectList(req);
            return result;
        }


        /// <summary>
        /// 查询模块分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "module:list")]
        public virtual async Task<PagedInfo<ModuleListItemDto>> GetModulePageList([FromBody] GetModulePageListReq req)
        {
            var result = await _ModuleService.GetModulePageList(req);
            return result;
        }

        /// <summary>
        /// 查询模块详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "module:detail")]
        public virtual async Task<ModuleDetailDto> GetModuleDetail(long id)
        {
            var result = await _ModuleService.GetModuleDetail(id);
            return result;
        }

    }
}